<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8" />
    <title>Document</title>
    <style></style>
  </head>
  <body>
    <div>MayBe函子</div>
    <p>我们在编程的过程中肯会遇到很多错误，需要对这些错误做相对于的处理</p>
    <p>MayBe函子的作用就是可以对外部的空值情况做处理(控制副作用在允许的范围)</p>
    <h2>
      MayBe函子是问题是多次调用map方法时传入null时打印的结果并不知道在哪里打印的
    </h2>
    <script>
      class MayBe {
        static of(value) {
          return new MayBe(value);
        }
        constructor(value) {
          this._value = value;
        }
        map(fn) {
          return this.isNothing() ? MayBe.of(null) : MayBe.of(fn(this._value));
        }
        isNothing() {
          return this._value === null || this._value === undefined;
        }
      }

      let r = MayBe.of(null);
      console.log(r);

      let r2 = MayBe.of("Hello World")
        .map((x) => x.toUpperCase())
        .map((x) => null)
        .map((x) => x.split(" "));
      console.log(r2); //此时返回了null   不知道在哪里开始返回了
    </script>
  </body>
</html>
